package cn.anc.aonicardv.module.adpter.album;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;


import java.io.File;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;
import cn.anc.aonicardv.GlideApp;
import cn.anc.aonicardv.R;
import cn.anc.aonicardv.bean.FileBean;
import cn.anc.aonicardv.module.adpter.listener.OnThumbnailListItemClickListener;
import cn.anc.aonicardv.widget.SlideSelectView;

/**
 * Created by yangdai on 2017/5/19.
 */

public class ThumbnailListAdapter extends RecyclerView.Adapter {

    protected List<? extends FileBean> fileBeanList;
    protected SlideSelectView slideSelectView;
    protected Context context;

    public ThumbnailListAdapter(Context context,List<? extends FileBean> fileBeanList, SlideSelectView slideSelectView) {
        this.fileBeanList = fileBeanList;
        this.slideSelectView = slideSelectView;
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_thumbnail, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final ViewHolder viewHolder = (ViewHolder) holder;
        if (slideSelectView.isUpdateItem(position)) {
            viewHolder.thumbnailIv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onThumbnailListItemClickListener != null)
                        onThumbnailListItemClickListener.onItemClick(slideSelectView.getRightItemPosition(position));
                }
            });
            GlideApp.with(context).load(Uri.fromFile( new File( fileBeanList.get(slideSelectView.getRightItemPosition(position)).getPath()))).placeholder(R.mipmap.image_loading).into(  viewHolder.thumbnailIv);

        } else {
            viewHolder.thumbnailIv.setOnClickListener(null);
            viewHolder.thumbnailIv.setImageBitmap(null);
        }
    }

    public void setData(List<FileBean> photoBeanList) {
        this.fileBeanList = photoBeanList;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public int getItemCount() {
        return fileBeanList == null ? 0 : slideSelectView.getRealItemCount(fileBeanList.size());
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView thumbnailIv;

        public ViewHolder(View view) {
            super(view);
            thumbnailIv = view.findViewById(R.id.iv_thumbnail);
        }
    }

    protected OnThumbnailListItemClickListener onThumbnailListItemClickListener;

    public void setOnThumbnailListItemClickListener(OnThumbnailListItemClickListener listener) {
        onThumbnailListItemClickListener = listener;
    }

}
